package com.huadao.cart_ccs.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.daolion.base.BsActivity;
import com.daolion.base.utils.ActivityUtil;
import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.common.RockToast;
import com.huadao.cart_ccs.common.UtilHelpers;
import com.huadao.cart_ccs.common.dialog.DialogFragmentHelper;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends BsActivity implements BaseView {
    protected final String TAG = getClass().getName();


    protected Toolbar toolbar;
    private ProgressDialog mProgressDialog;
    private Unbinder butterBinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        butterBinder = ButterKnife.bind(this);
    }

    public void preOnCreate() {

        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setCancelable(true);
        Window window = mProgressDialog.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        window.setAttributes(layoutParams);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }


    @Override
    public void showLoading(String msg) {
        if (!mProgressDialog.isShowing()) {
            if (TextUtils.isEmpty(msg)) {
                msg = getString(R.string.loading);
            }
            mProgressDialog.setMessage(msg);
            mProgressDialog.show();
        }
    }

    protected static <T extends BaseActivity> void launch(Context context, Class<T> tClass) {
        Intent intent = new Intent(context, tClass);
        ActivityUtil.switchTo(context, intent);
    }

    @Override
    public void hideLoading() {
        if (mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void showToast(String msg) {
        RockToast.showToastSafe(msg);
    }

    @Override
    public void showDialogMessage(String title, String msg) {
        if (TextUtils.isEmpty(title)) {
            title = "";
        }
        DialogFragmentHelper.showMessageDialog(this, title, msg);
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                View view = getCurrentFocus();
                UtilHelpers.hideKeyboard(ev, view, BaseActivity.this);//调用方法判断是否需要隐藏键盘
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }


    @Override
    protected void onDestroy() {
        hideLoading();
        super.onDestroy();
        if (null != butterBinder) {
            butterBinder.unbind();
        }
    }

}
